package com.tengju.settlement.domain.model.depository;

import com.tengju.settlement.domain.model.account.UserIdCode;
import com.tengju.settlement.domain.shared.DomainException;
import com.tengju.settlement.domain.shared.DomainExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.experimental.NonFinal;

@Value
@AllArgsConstructor
public class DepositoryAccount {

    DepositoryAccountId depositoryAccountId;

    @NonFinal
    UserIdCode idCode;

    DepositoryChannel depositoryChannel;

    License license;

    String depositoryId;

    @NonFinal
    AccountStatus status;

    public boolean compareAccountLicense(License license) {
        if (license == null) {
            return false;
        }
        return this.license.getLicenseType() == license.getLicenseType()
                && this.license.getLicenseNumber().equals(license.getLicenseNumber());
    }

    public void changeStatus(AccountStatus afterStatus) {
        if ((AccountStatus.NORMAL == status ||
                AccountStatus.CARD_NOT_BIND == status ||
                AccountStatus.CARD_BINDING == status)
                && AccountStatus.LOGICAL_CANCEL == afterStatus) {
            status = AccountStatus.LOGICAL_CANCEL;
        } else if (AccountStatus.LOGICAL_CANCEL == status && AccountStatus.CARD_NOT_BIND == afterStatus) {
            status = AccountStatus.CARD_NOT_BIND;
        } else if ((AccountStatus.CARD_NOT_BIND == status || AccountStatus.LOGICAL_CANCEL == status) && AccountStatus.CARD_BINDING == afterStatus) {
            status = AccountStatus.CARD_BINDING;
        } else if ((AccountStatus.CARD_BINDING == status || AccountStatus.CARD_NOT_BIND == status || AccountStatus.LOGICAL_CANCEL == status)
                && AccountStatus.NORMAL == afterStatus) {
            status = AccountStatus.NORMAL;
        } else {
            throw DomainException.error(DomainExceptionCode.DEP_ACCOUNT_STATUS_CHANGE_FAIL);
        }
    }

    public void putIdCode(UserIdCode idCode) {
        this.idCode = idCode;
    }



    public void logicalCancel() {
        changeStatus(AccountStatus.LOGICAL_CANCEL);
    }

}
